package com.test.followme;

import com.google.android.gms.maps.model.LatLng;
import com.test.user.OwnUser;
import com.test.utilities.Utility;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class Splash extends Activity
{
	RegId reg;
	OwnUser me;
	Context context;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash);
		Globals.isWorking = true;
		
		context = getApplicationContext();
		
		reg = new RegId(context);
		
		Thread timer = new Thread()
		{
			public void run()
			{
				try
				{
					reg.register2GCM();

					sleep(4000);
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					Intent openStartPoint;
					String ssnId = Utility.getSharedPrefs(context, Globals.PREFS_SESSION_ID);
					
					Log.i("Splash", "ssnId : " + ssnId);
					
					if ( true   /* !Utility.isNullOrEmpty(ssnId) */)
					{
						Log.i("Splash", "ssnId is not null");
						me = new OwnUser();
						FillOwnUser();
						
						openStartPoint = new Intent(getApplicationContext(), MainMap.class);
						openStartPoint.putExtra("OwnUser", me);
					}
					else
					{
						Log.i("Splash", "ssnId is null");
						openStartPoint = new Intent(getApplicationContext(), LoginActivity.class);
					}
					startActivity(openStartPoint);
				}
			}
		};
		
		timer.start();
	}
	
	void FillOwnUser()
	{	
		me.setGcmRegId(Utility.getSharedPrefs(context, Globals.PREFS_REG_ID));
		me.setId(Utility.getSharedPrefs(context, Globals.PREFS_USER_ID));
		me.setName(Utility.getSharedPrefs(context, Globals.PREFS_USER_NAME));
		me.setMail(Utility.getSharedPrefs(context, Globals.PREFS_EMAIL));
		me.setPwd(Utility.getSharedPrefs(context, Globals.PREFS_PWD));
		me.setSsnId(Utility.getSharedPrefs(context, Globals.PREFS_SESSION_ID));
		me.setOnline(Boolean.valueOf(Utility.getSharedPrefs(context, Globals.PREFS_ONLINE_STATUS)));
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		Globals.isWorking = false;
		finish();
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}
}
